const app = getApp();

var wdlist = [{
  "id": 1,
  "name": "房源收藏",
  "time": "2016/08/25 13:00",
  "reserver": "大白牙就是小明收藏了A02",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url":"../scxq/scxq"
},
{
  "id": 2,
  "name": "合同修改",
  "time": "2016/08/25 13:00",
  "reserver": "张三同意了合同尾号为4396的修改",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url": "../htxgtz/htxgtz"
},
{
  "id": 3,
  "name": "房源收藏",
  "time": "2016/08/25 13:00",
  "reserver": "大白牙就是小明收藏了A02",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url":"../scxq/scxq"
},
{
  "id": 4,
  "name": "合同修改",
  "time": "2016/08/25 13:00",
  "reserver": "张三同意了合同尾号为4396的修改",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url": "../htxgtz/htxgtz"
},
{
  "id": 5,
  "name": "合同修改",
  "time": "2016/08/25 13:00",
  "reserver": "张先生",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url": "../htxgtz/htxgtz"
},
{
  "id": 6,
  "name": "合同修改",
  "time": "2016/08/25 13:00",
  "reserver": "张先生",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg",
  // "url": "../htxgtz/htxgtz"
},
{
  "id": 7,
  "name": "合同修改",
  "time": "2016/08/25 13:00",
  "reserver": "张先生",
  // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg"
}]

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    tabs: ["未读", "已读"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    wdlist: [],
    ydlist: [],
    height: "",
    isHideLoadMore: true,
    isRefresh: true,
    page: 1
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
          height: res.windowHeight / 1.1 + "px"
        });
      }
    });
    this.fetchData();
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  fetchData: function (flag) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.url + '/wx/house/houselist',
      data: {
        token: wx.getStorageSync('token'),
        status: 1,
        houseId: "1",
        page: this.data.page
      },
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        if (res.data.success == true) {
          if (flag == "refresh") {
            wx.showToast({
              title: '刷新成功',
              icon: 'success',
              duration: 3000
            });
          }
          var newWdlist = wdlist;
          if (that.data.page != 1) {
            console.log(that.data.page);
            var json = [{
              "id": 8,
              "name": "合同修改",
              "time": "2016/08/25 13:00",
              "reserver": "张先生",
              // "imgurl": "http://pic.58pic.com/58pic/12/34/51/85d58PICkjf.jpg"
            }];
            newWdlist=newWdlist.concat(json)
          }

          wdlist = newWdlist;
          console.log(wdlist);

          for (var i = 0; i < wdlist.length; i++) {
            if (wdlist[i].name == "合同修改") {
              wdlist[i]["imgurl"] = "../../image/hetong.png"
            } else {
              wdlist[i]["imgurl"] = "../../image/shoucang.png"
            }
          }

          that.setData({
            wdlist: wdlist
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            image: '../../image/shibai.png',
            duration: 2000
          })
        }
      },
      fail: res => {
        wx.showToast({
          title: '消息获取失败',
          image: '../../image/shibai.png',
          duration: 2000
        })
      },
      complete: res => {
        wx.hideLoading(),
          that.setData({
            isHideLoadMore: true,
            isRefresh: true
          })
      }
    })
  },
  toDetail: function (e) {
    var that = this;
    if (e.currentTarget.dataset.name == "房源收藏") {
      wx.navigateTo({
        url: '../scxq/scxq?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '../htxgtz/htxgtz?id=' + e.currentTarget.dataset.id,
      })
    }
    wx.request({
      url: app.globalData.url + '/wx/house/houselist',
      data: {
        token: wx.getStorageSync('token'),
        flag: 1,
        xxid: 1
      },
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        var newWdList = that.data.wdlist;
        var newYdList = that.data.ydlist;
        if (res.data.success == true) {
          for (var i = 0; i < that.data.wdlist.length; i++) {
            if (e.currentTarget.dataset.id == that.data.wdlist[i].id) {
              newYdList.splice(0, 0, that.data.wdlist[i]);
              newWdList.splice(i, 1);

            }
          }
          console.log(newYdList);
          that.setData({
            wdlist: newWdList,
            ydlist: newYdList
          })
        } else {
          wx.showToast({
            title: '状态更改失败',
          })
        }
      },
      fail: res => {
        wx.showToast({
          title: '状态更改失败',
        })
      }
    })
  },
  loadMore: function () {
    var newPage = this.data.page + 1;
    this.setData({
      isHideLoadMore: false,
      page: newPage
    })
    this.fetchData();
  },
  refresh: function () {
    this.setData({
      isRefresh: false,
      page: 1
    })
    this.fetchData("refresh");
  }
});


